<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin', 'admin', 0);

class picture_data extends admin
{
    function __construct()
    {
        parent::__construct();
        $this->M = new_html_special_chars(getcache('picture_fast', 'commons'));
        $this->db = pc_base::load_model('picture_fast_model');
        $this->username = param::get_cookie('admin_username');
        $this->ddb2 = pc_base::load_model('type_model');
        $this->cdb = pc_base::load_model('category_model');
        $this->ddb= pc_base::load_model('picture_fast_data_model');
    }

    public function init()
    {
//        $where = array('siteid' => $this->get_siteid());
        $where = array();
        if (empty($_GET['pf_id'])) {
            showmessage(L('pf_id'), HTTP_REFERER);
        }else{
            $where ['pf_id'] = intval($_GET['pf_id']);
        }
        $page = isset($_GET['page']) && intval($_GET['page']) ? intval($_GET['page']) : 1;
        $infos = $this->ddb->listinfo($where, $order = 'listorder DESC,id DESC', $page, $pages = '9');
        $pages = $this->ddb->pages;
        $pf_id = intval($_GET['pf_id']);
        $data= $this->db->get_one(array('pf_id' => $pf_id));
        $title=is_array($data)?$data['title']:'';
        $big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m=picture_fast&c=picture_data&a=add&pf_id='.$pf_id.'\', title:\'' . L('picture_data_add') . '\', width:\'700\', height:\'450\'}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', L('picture_data_add'));
        extract($_GET);
        include $this->admin_tpl('data_list');
    }

    //添加图集
    public function add()
    {
        if (isset($_POST['dosubmit'])) {
            if (empty($_POST['data']['thumb'])) {
                showmessage(L('input').L('data_thumb'), HTTP_REFERER);
            } else {
                $_POST['data']['thumb'] = safe_replace($_POST['data']['thumb']);
            }
            if (empty($_POST['pf_id'])) {
                showmessage(L('pf_id'), HTTP_REFERER);
            } else {
                $_POST['data']['pf_id'] = intval($_POST['pf_id']);
            }
            $data = new_addslashes($_POST['data']);
            $id = $this->ddb->insert($data, true);
            if (!id) return FALSE;
            $siteid = $this->get_siteid();
            //更新附件状态
            if (pc_base::load_config('system', 'attachment_stat') & $_POST['data']['thumb']) {
                $this->attachment_db = pc_base::load_model('attachment_model');
                $this->attachment_db->api_update($_POST['data']['thumb'], 'picture_fast_data-' . $id, 1);
            }
            showmessage(L('operation_success'), HTTP_REFERER, '', 'add');
        } else {
            $show_validator = $show_scroll = $show_header = true;
            pc_base::load_sys_class('form', '', 0);
            $siteid = $this->get_siteid();
            if (empty($_GET['pf_id'])) {
                showmessage(L('input').L('pf_id'), HTTP_REFERER);
            }
            $pf_id = intval($_GET['pf_id']);


            //print_r($types);exit;
            include $this->admin_tpl('data_add');
        }

    }

    /**
     * 说明:异步更新排序
     * @param  $optionid
     */
    public function listorder_up()
    {
        $result = $this->ddb->update(array('listorder' => '+=1'), array('id' => $_GET['id']));
        if ($result) {
            echo 1;
        } else {
            echo 0;
        }
    }

    //更新排序
    public function listorder()
    {
        if (isset($_POST['dosubmit'])) {
            foreach ($_POST['listorders'] as $pf_id => $listorder) {
                $id = intval($id);
                $this->ddb->update(array('listorder' => $listorder), array('id' => $id));
            }
            showmessage(L('operation_success'), HTTP_REFERER);
        }
    }



    public function edit()
    {
        if (isset($_POST['dosubmit'])) {
            $id = intval($_GET['id']);
            if ($id < 1) return false;
            if (!is_array($_POST['data']) || empty($_POST['data'])) return false;
            if ((!$_POST['data']['thumb']) || empty($_POST['data']['thumb'])) return false;
            $this->ddb->update($_POST['data'], array('id' => $id));
            //更新附件状态
            if (pc_base::load_config('system', 'attachment_stat') & $_POST['data']['thumb']) {
                $this->attachment_db = pc_base::load_model('attachment_model');
                $this->attachment_db->api_update($_POST['data']['thumb'], 'picture_fast_data-' . $id, 1);
            }
            showmessage(L('operation_success'), '?m=picture_fast&c=picture_data&a=edit', '', 'edit');

        } else {
            $siteid = $this->get_siteid();
            $show_validator = $show_scroll = $show_header = true;
            pc_base::load_sys_class('form', '', 0);

//            $template_list = template_list($siteid, 0);
//            foreach ($template_list as $k => $v) {
//                $template_list[$v['dirname']] = $v['name'] ? $v['name'] : $v['dirname'];
//                unset($template_list[$k]);
//            }

            //解出链接内容
            $info = $this->ddb->get_one(array('id' => $_GET['id']));
            if (!$info) showmessage(L('data_exit'));
            extract($info);
            include $this->admin_tpl('data_edit');
        }

    }


    /**
     * 删除图集
     * @param intval $sid 图集ID，递归删除
     */
    public function delete()
    {
        if ((!isset($_GET['id']) || empty($_GET['id'])) && (!isset($_POST['id']) || empty($_POST['id']))) {
            showmessage(L('illegal_parameters'), HTTP_REFERER);
        } else {
            if (is_array($_POST['id'])) {
                foreach ($_POST['id'] as $pf_id_arr) {

                    //批量删除图集
                    $this->ddb->delete(array('id' => $pf_id_arr));
                    //更新附件状态
                    if (pc_base::load_config('system', 'attachment_stat')) {
                        $this->attachment_db = pc_base::load_model('attachment_model');
                        $this->attachment_db->api_delete('picture_fast_data-' . $pf_id_arr);
                    }
                }
                showmessage(L('operation_success'), '?m=picture_fast&c=picture_data&pf_id='.$_GET['pf_id']);
            } else {
                $pf_id = intval($_GET['id']);
                if ($pf_id < 1) return false;
                //删除图集
                $result = $this->ddb->delete(array('id' => $pf_id));
                //更新附件状态
                if (pc_base::load_config('system', 'attachment_stat')) {
                    $this->attachment_db = pc_base::load_model('attachment_model');
                    $this->attachment_db->api_delete('picture_fast_data-' . $pf_id);
                }
                if ($result) {
                    showmessage(L('operation_success'), '?m=picture_fast&c=picture_data&pf_id='.$_GET['pf_id']);
                } else {
                    showmessage(L("operation_failure"), '?m=picture_fast&c=picture_data&pf_id='.$_GET['pf_id']);
                }
            }
            showmessage(L('operation_success'), HTTP_REFERER);
        }
    }



    /**
     * 说明:对字符串进行处理
     * @param $string 待处理的字符串
     * @param $isjs 是否生成JS代码
     */
    function format_js($string, $isjs = 1)
    {
        $string = addslashes(str_replace(array("\r", "\n"), array('', ''), $string));
        return $isjs ? 'document.write("' . $string . '");' : $string;
    }


}

?>